capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

/* 
RD estimates (persist)
*/


local exp "prworapersist";

local fs_vars  "age18_yes n_age18unfav n_onssi1922 n_onssi1922_exact n_onssi20 " ;
local hh_vars "n_earnhh9702 n_earnhh9712 n_emphhpost9702 n_emphhpost9712 n_inchh9702 n_inchh9712" ;
local covariates "n_male n_firstage n_fam_singmom n_fam_nopar n_dibmdr_mie n_dibmdr_mine n_dibmdr_miss n_diag1_mental n_diag1_nerv n_diag1_sense n_diag1_infec n_diag1_endo n_diag1_none n_diag1_cong n_diag1_musc n_diag1_resp n_diag1_blood n_diag1_neo n_earnhhpre9095" ;
local new_vars "*_n1early*" ;

local controls_all "n_male i.n_firstage i.n_diag1cat n_dibmdr_mie n_dibmdr_mine n_dibmdr_miss n_fam_singmom n_fam_nopar i.n_earnhhbins i.state" ;
local controls_no  "n_dibmdr_mie n_dibmdr_mine n_dibmdr_miss" ; 

local bwidth_list "264" ;
local cov_list   "all" ; /* no */
local demo_main "all male female" ;
local demo_oth "singno twopar parhigh parlow othmen nonment intel phys nonphys white black nonw" ;

foreach demo in `demo_main' { ;
	local vars_`demo' "order_* tm* " ; 
} ;
foreach demo in `demo_oth' { ;
	local vars_`demo' "n_crime* order_* avg_*" ;
} ;

local poly_list "1(1)1" ;

use "`crime'\prep2\crime_prwora_prepforreg_persist.dta", clear ;

keep if abs(dobdd_run) <= `bwidth_list' ;

/* Subsamples */
gen demo_all = 1 ;
gen byte demo_othment	= (n_diag1_mental == 1 & (substr(dibdig,1,3) != "317" & substr(dibdig,1,3) != "318" & substr(dibdig,1,3) != "319" )) ;
gen byte demo_nonment 	= (demo_othment == 0) ;
gen byte demo_intel 	= (n_diag1_mental == 1 & (substr(dibdig,1,3) == "317" | substr(dibdig,1,3) == "318" | substr(dibdig,1,3) == "319" )) ;
gen byte demo_phys 		= (n_diag1_mental != 1) ;
gen byte demo_nonphys 	= (n_diag1_mental == 1) ;
gen byte demo_twopar 	= (n_fam_singmom != 1 & n_fam_nopar != 1) ;
gen byte demo_singno 	= (n_fam_singmom == 1) | (n_fam_nopar ==1) ;
gen byte demo_nopar  	= (n_fam_nopar == 1) ;
gen byte demo_male 	 	= (n_male ==1) ;
gen byte demo_female 	= (n_male !=1) ;
gen byte demo_white		= (race == "W") ;
gen byte demo_black 	= (race == "B") ;
gen byte demo_nonw		= (race != "W") ;

egen cnty_ue97 = rowmean(cnty_urate_1997-cnty_urate_1998) ;
egen cnty_uegr = rowmean(cnty_urate_2007-cnty_urate_2012) ;
egen cnty_ueall = rowmean(cnty_urate_1997-cnty_urate_2017) ;
rename cnty_crimerate cnty_crime ;
rename cnty_share_blue cnty_dem ;
rename cnty_pop2000 cnty_pop ;

foreach cnty in crime dem pop ue97 uegr ueall { ;
	summ cnty_`cnty', det ;
	gen byte demo_`cnty'high = (cnty_`cnty' > r(p50) & cnty_`cnty' != .) ;
	gen byte demo_`cnty'low = (cnty_`cnty' <= r(p50) & cnty_`cnty' != .) ;
	replace demo_`cnty'high = . if cnty_`cnty' == . ;
	replace demo_`cnty'low = . if cnty_`cnty' == . ;
	tab demo_`cnty'high, m ;
	tab demo_`cnty'low, m ;
} ;

summ n_earnhhpre9095, det ;
replace n_earnhhpre9095 = 0 if n_earnhhpre9095 == . ;
gen byte demo_parhigh = (n_earnhhpre9095 > r(p50) & n_earnhhpre9095 != .) ;
gen byte demo_parlow = (n_earnhhpre9095 <= r(p50) & n_earnhhpre9095 != .) ;
replace demo_parhigh = . if n_earnhhpre9095 == . ;
replace demo_parlow = . if n_earnhhpre9095 == . ;
tab demo_parhigh, m ;
tab demo_parlow, m ;

/* RD variables */
gen dobdd_run2=dobdd_run*dobdd_run;
gen dobdd_run3=dobdd_run2*dobdd_run;
gen dobdd_run4=dobdd_run3*dobdd_run;

gen postXdobdd=n_post*dobdd_run;
gen postXdobdd2=n_post*dobdd_run2;
gen postXdobdd3=n_post*dobdd_run3;
gen postXdobdd4=n_post*dobdd_run4;

local poly_1 "dobdd_run postXdobdd";
local poly_2 "dobdd_run dobdd_run2 postXdobdd postXdobdd2";
local poly_3 "dobdd_run dobdd_run2 dobdd_run3 postXdobdd postXdobdd2 postXdobdd3";
local poly_4 "dobdd_run dobdd_run2 dobdd_run3 dobdd_run4 postXdobdd postXdobdd2 postXdobdd3 postXdobdd4";

foreach demo in `demo_main' { ;

foreach bwidth in `bwidth_list' {;

	forval poly = `poly_list' {;
		
		foreach cov in `cov_list' { ;
		
				
				/* RD regressions */
				
				reg n_post n_post `poly_`poly'' 
					if abs(dobdd_run)<=`bwidth' & demo_`demo' == 1 
					, robust;
					outreg2 using "`output'\\`exp'_bw`bwidth'_crime_poly`poly'`cov'cov_rd_`demo'.xls", e(N r2 rmse) replace;
				
				foreach var of varlist 
					`vars_`demo''
					{;
					xi: reg `var' n_post `poly_`poly''
						`controls_`cov''
						if abs(dobdd_run)<=`bwidth' & demo_`demo' == 1
						, robust; 
					outreg2 using "`output'\\`exp'_bw`bwidth'_crime_poly`poly'`cov'cov_rd_`demo'.xls", e(N r2 rmse) append;
					} ;	
					
		
		/* IV estimates */
				
				ivreg2 n_post (n_age18unfav=n_post) `poly_`poly''
					if abs(dobdd_run)<=`bwidth' & demo_`demo' == 1
					, robust;
					outreg2 using  "`output'\\`exp'_bw`bwidth'_crime_poly`poly'`cov'cov_iv_`demo'.xls", e(N r2 rmse) replace;
				
				/* IV using unfavorable age 18 */
				foreach var of varlist 
					`vars_`demo''
					{;
					
					qui summ `var' 
						if abs(dobdd_run)<=`bwidth' & demo_`demo' == 1 ;
					if r(sd) != 0 { ;
						xi: ivreg2 `var' (n_age18unfav=n_post) `poly_`poly''
							`controls_`cov''
							if abs(dobdd_run)<=`bwidth' & demo_`demo' == 1
							, robust;
						outreg2 using  "`output'\\`exp'_bw`bwidth'_crime_poly`poly'`cov'cov_iv_`demo'.xls", e(N r2 rmse) append;
						} ;
					};
				
			} ; /* end cov */
			
		} ; /* end poly */
	} ; /* end bwidth*/
} ;	/* end demo */


capture log close;
